package com.ruyuan.eshop.manager.controller.trade;

import com.ruyuan.eshop.framework.core.WebPage;
import com.ruyuan.eshop.common.enums.OrderPermission;
import com.ruyuan.eshop.common.domain.entity.OrderLogDO;
import com.ruyuan.eshop.common.domain.dto.OrderQueryParam;
import com.ruyuan.eshop.common.domain.vo.CancelVO;
import com.ruyuan.eshop.common.domain.vo.OrderDetailVO;
import com.ruyuan.eshop.common.service.trade.order.OrderLogService;
import com.ruyuan.eshop.common.service.trade.order.OrderOperateService;
import com.ruyuan.eshop.common.service.trade.order.OrderQueryService;
import com.ruyuan.eshop.manager.domain.query.OrderListQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 平台订单API
 *
 * @author pwd
 * @version 1.0
 */
@Api(description = "平台订单API")
@RestController
@RequestMapping("/admin/v1.0/trade/orders")
@Validated
public class OrderManagerController {

    @Autowired
    private OrderQueryService orderQueryService;

    @Autowired
    private OrderOperateService orderOperateService;

    @Autowired
    private OrderLogService orderLogService;

    /**
     * 查询订单列表
     * @param orderListQuery
     * @return
     */
    @ApiOperation(value = "查询订单列表")
    @GetMapping
    public WebPage list(OrderListQuery orderListQuery) {

        OrderQueryParam param = new OrderQueryParam();
        param.setOrderSn(orderListQuery.getOrderSn());
        param.setShipName(orderListQuery.getShipName());
        param.setGoodsName(orderListQuery.getGoodsName());
        param.setBuyerName(orderListQuery.getBuyerName());
        param.setOrderStatus(orderListQuery.getOrderStatus());
        param.setStartTime(orderListQuery.getStartTime());
        param.setEndTime(orderListQuery.getEndTime());
        param.setPageNo(orderListQuery.getPageNo());
        param.setPageSize(orderListQuery.getPageSize());
        param.setMemberId(orderListQuery.getMemberId());
        param.setSellerId(orderListQuery.getSellerId());
        param.setPaymentType(orderListQuery.getPaymentType());
        param.setClientType(orderListQuery.getClientType());

        WebPage page = this.orderQueryService.list(param);
        return page;
    }

    /**
     * 查询单个订单明细
     * @param orderSn
     * @return
     */
    @ApiOperation(value = "查询单个订单明细")
    @GetMapping(value = "/{order_sn}")
    public OrderDetailVO get(@PathVariable("order_sn") String orderSn) {
        OrderDetailVO detailVO = this.orderQueryService.getModel(orderSn, null);
        if (detailVO.getNeedReceipt().intValue() == 1) {
            detailVO.setReceiptHistory(null);
        }
        return detailVO;
    }

    /**
     * 确认收款
     * @param orderSn
     * @param payPrice
     * @return
     */
    @ApiOperation(value = "确认收款")
    @PostMapping(value = "/{order_sn}/pay")
    public String payOrder(@PathVariable("order_sn") String orderSn, Double payPrice) {
        this.orderOperateService.payOrder(orderSn, payPrice, "", OrderPermission.admin);
        return "";
    }

    /**
     * 取消订单
     * @param orderSn
     * @return
     */
    @ApiOperation(value = "取消订单")
    @PostMapping(value = "/{order_sn}/cancelled")
    public String cancelledOrder(@PathVariable("order_sn") String orderSn) {

        CancelVO cancelVO = new CancelVO();
        cancelVO.setReason("管理员取消");
        cancelVO.setOrderSn(orderSn);
        cancelVO.setOperator("平台管理员");

        this.orderOperateService.cancel(cancelVO, OrderPermission.admin);
        return "";
    }

    /**
     * 查询订单日志
     * @param orderSn
     * @return
     */
    @ApiOperation(value = "查询订单日志")
    @GetMapping(value = "/{order_sn}/log")
    public List<OrderLogDO> getList(@PathVariable("order_sn") String orderSn) {
        List<OrderLogDO> logDOList = this.orderLogService.listAll(orderSn);
        return logDOList;
    }

}
